﻿/***********************************************************************************************
 * 
 * ProjectName: WMSSoft.Lib.Interfaces
 * 
 * Description: Provide the Interface to remove a File from Recent-List
 * 
 * Created:     30.08.2015
 * 
 * Author:      matthias wagler - matthias@WMSSoft.de
 * 
 * Copyright:   Copyright 2015 by WMSSoft.de
 * 
 * License:     GPLv3
 * 
 * History:     30.08.2015 - matthias wagler matthias@WMSSoft.de   -   Created
 * 
 **********************************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WMSSoft.Lib.Basic;
using WMSSoft.Lib.Texte;
using WMSSoft.Lib.WinKStart;
using WMSSoft.Lib.WinKStart.Classes.Metadata;
using WMSSoft.Lib.WinKStart.Abstract;
using WMSSoft.Lib.WinKStart.Model.MenuConfig;
using WMSSoft.Lib.WinKStart.Model;
using WMSSoft.Lib.WinKStart.ViewModel;
using WMSSoft.Lib.WinKStart.Model.Metadata;

namespace WMSSoft.WinKStart.ViewModel
{
    public class Settings_NewEditDataSourceViewModel : SettingBaseListViewModel
    {
        public event EventHandler DataChanged;
        List<KeyValueAssignment> _AvailableAssemblies = null;
        List<KeyValueAssignment> _DataSourceOptions = null;
        List<KeyValueAssignment> _CustomAssignments = null;
        List<KeyValueAssignment> _AssemblyClasses = null;
        Dictionary<string, CMeta> _AssemblyMetaData = null;
        List<cStartmenuItem> _CustomItems = null;
        DataSource _Data = null;
        KeyValueAssignment _SelectedOption = null;
        KeyValueAssignment _SelectedAssignment = null;
        cStartmenuItem _SelectedCustomItem = null;
        string _SelectedAssembly = "";
        string _SelectedClass = "";
        string _DataSourceName = "";
        string _DataSourceShortName = "";
        bool _DataChanged = false;
        bool _OptionSelected = false;
        bool _CustomItemSelected = false;
        bool _CustomAssignmentSelected = false;
        bool _Searchable = false;

        public Settings_NewEditDataSourceViewModel() 
        {
            base.ViewName = LanguageStrings.GetText("New DataSource");

            PropertyChanged += Settings_NewEditDataSourceViewModel_PropertyChanged;

            _AvailableAssemblies = LoadInstallableDataSources();

            Data = new DataSource();
            DataSourceOptions = new List<KeyValueAssignment>();
            CustomAssignments = new List<KeyValueAssignment>();
            CustomItems = new List<cStartmenuItem>();

            ViewModel_AddClick += Settings_NewEditDataSourceViewModel_ViewModel_AddClick;
            ViewModel_EditClick += Settings_NewEditDataSourceViewModel_ViewModel_EditClick;
            ViewModel_DeleteClick += Settings_NewEditDataSourceViewModel_ViewModel_DeleteClick;

            ViewModel_ApplyClick +=Settings_NewEditDataSourceViewModel_ViewModel_ApplyClick;
            ViewModel_OkClick += Settings_NewEditDataSourceViewModel_ViewModel_OkClick;
            ViewModel_CloseClick += Settings_NewEditDataSourceViewModel_ViewModel_CloseClick;

            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Edit"), new CDelegatingCommand(Delegate => this.Cmd_Exec_EditOption())));

            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Add"), new CDelegatingCommand(Delegate => this.Cmd_Exec_AddAssignment())));
            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Edit"), new CDelegatingCommand(Delegate => this.Cmd_Exec_EditAssignment())));
            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Delete"), new CDelegatingCommand(Delegate => this.Cmd_Exec_DeleteAssignment())));

        }


              
        public Settings_NewEditDataSourceViewModel(DataSource Data)
        {
            base.ViewName = LanguageStrings.GetText("Edit DataSource");

            PropertyChanged += Settings_NewEditDataSourceViewModel_PropertyChanged;

            _AvailableAssemblies = LoadInstallableDataSources();

            _Data = Data;
            DataSourceName = _Data.Name;
            DataSourceShortName = _Data.ShortName;
            SelectedAssembly = _Data.Assembly;
            SelectedClass = _Data.ClassName;
            LoadOptions();
            LoadCustomItems();
            LoadAssignments();
            Searchable = _Data.Searchable;
            //_CustomItems = _Data.Items;

            ViewModel_AddClick += Settings_NewEditDataSourceViewModel_ViewModel_AddClick;
            ViewModel_EditClick += Settings_NewEditDataSourceViewModel_ViewModel_EditClick;
            ViewModel_DeleteClick += Settings_NewEditDataSourceViewModel_ViewModel_DeleteClick;

            ViewModel_ApplyClick += Settings_NewEditDataSourceViewModel_ViewModel_ApplyClick;
            ViewModel_OkClick += Settings_NewEditDataSourceViewModel_ViewModel_OkClick;
            ViewModel_CloseClick += Settings_NewEditDataSourceViewModel_ViewModel_CloseClick;

            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Edit"), new CDelegatingCommand(Delegate => this.Cmd_Exec_EditOption())));

            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Add"), new CDelegatingCommand(Delegate => this.Cmd_Exec_AddAssignment())));
            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Edit"), new CDelegatingCommand(Delegate => this.Cmd_Exec_EditAssignment())));
            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Delete"), new CDelegatingCommand(Delegate => this.Cmd_Exec_DeleteAssignment())));
        }

        #region Properties

        public CSingleCommandViewModel Cmd_EditOptionClick
        {
            get { return m_oCommands[6]; }
        }

        public CSingleCommandViewModel Cmd_AddAssignmentClick
        {
            get { return m_oCommands[7]; }
        }

        public CSingleCommandViewModel Cmd_EditAssignmentClick
        {
            get { return m_oCommands[8]; }
        }

        public CSingleCommandViewModel Cmd_DeleteAssignmentClick
        {
            get { return m_oCommands[9]; }
        }

        public DataSource Data 
        {
            get { return _Data; }
            set
            {
                _Data = value;
                OnPropertyChanged("Data");
            }
        }

        public string DataSourceName
        {
            get { return _DataSourceName; }
            set
            {
                _DataSourceName = value;
                OnPropertyChanged("DataSourceName");
            }
        }

        public string DataSourceShortName
        {
            get { return _DataSourceShortName; }
            set
            {
                _DataSourceShortName = value;
                OnPropertyChanged("DataSourceShortName");
            }
        }

        public List<KeyValueAssignment> AvailableAssemblies {
            get { return _AvailableAssemblies; }
            set
            {
                _AvailableAssemblies = value;
                OnPropertyChanged("AvailableAssemblies");
            }
        }

        public string SelectedAssembly
        {
            get { return _SelectedAssembly; }
            set
            {
                _SelectedAssembly = value;
                OnPropertyChanged("SelectedAssembly");
            }
        }

        public string SelectedClass
        {
            get { return _SelectedClass; }
            set
            {
                _SelectedClass = value;
                OnPropertyChanged("SelectedClass");
            }
        }

        public List<KeyValueAssignment> AssemblyClasses
        {
            get { return _AssemblyClasses; }
            set
            {
                _AssemblyClasses = value;
                OnPropertyChanged("AssemblyClasses");
            }
        }

        public List<KeyValueAssignment> DataSourceOptions
        {
            get { return _DataSourceOptions; }
            set
            {
                _DataSourceOptions = value;
                OnPropertyChanged("DataSourceOptions");
            }
        }

        public KeyValueAssignment SelectedOption
        {
            get { return _SelectedOption; }
            set
            {
                _SelectedOption = value;
                OnPropertyChanged("SelectedOption");
            }
        }

        public bool OptionSelected
        {
            get { return _OptionSelected; }
            set
            {
                _OptionSelected = value;
                OnPropertyChanged("OptionSelected");
            }
        }

        public List<cStartmenuItem> CustomItems
        {
            get { return _CustomItems; }
            set
            {
                _CustomItems = value;
                OnPropertyChanged("CustomItems");
            }
        }
        public cStartmenuItem SelectedCustomItem
        {
            get { return _SelectedCustomItem; }
            set
            {
                _SelectedCustomItem = value;
                OnPropertyChanged("SelectedCustomItem");
            }
        }

        public bool CustomItemSelected
        {
            get { return _CustomItemSelected; }
            set
            {
                _CustomItemSelected = value;
                OnPropertyChanged("CustomItemSelected");
            }
        }

        public List<KeyValueAssignment> CustomAssignments
        {
            get { return _CustomAssignments; }
            set
            {
                _CustomAssignments = value;
                OnPropertyChanged("CustomAssignments");
            }
        }

        public KeyValueAssignment SelectedAssignment
        {
            get { return _SelectedAssignment; }
            set
            {
                _SelectedAssignment = value;
                OnPropertyChanged("SelectedAssignment");
            }
        }

        public bool CustomAssignmentSelected
        {
            get { return _CustomAssignmentSelected; }
            set
            {
                _CustomAssignmentSelected = value;
                OnPropertyChanged("CustomAssignmentSelected");
            }
        }

        public bool Searchable
        {
            get { return _Searchable; }
            set
            {
                _Searchable = value;
                OnPropertyChanged("Searchable");
            }
        }

        #endregion


        #region Command Procedures
        void Settings_NewEditDataSourceViewModel_ViewModel_AddClick(object sender, EventArgs e)
        {
            try
            {
                System.Windows.Controls.ListView tmpListView = null;
                if ((System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_AddClick.Command).CommandParameter != null)
                {
                    tmpListView = (System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_AddClick.Command).CommandParameter;
                    Settings_NewEditDSViewModel tmpViewModel = new Settings_NewEditDSViewModel();
                    Settings_NewEditDSCustomItem tmpDialog = new Settings_NewEditDSCustomItem();
                    tmpDialog.DataContext = tmpViewModel;
                    tmpDialog.ShowDialog();
                }
            }
            catch (Exception Ex)
            {

            }
        }

        void Settings_NewEditDataSourceViewModel_ViewModel_EditClick(object sender, EventArgs e)
        {
            try
            {
                if (SelectedCustomItem != null)
                {
                    System.Windows.Controls.ListView tmpListView = null;
                    if ((System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_EditClick.Command).CommandParameter != null)
                    {
                        tmpListView = (System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_EditClick.Command).CommandParameter;
                        Settings_NewEditDSViewModel tmpViewModel = new Settings_NewEditDSViewModel(SelectedCustomItem);
                        Settings_NewEditDSCustomItem tmpDialog = new Settings_NewEditDSCustomItem();
                        tmpDialog.DataContext = tmpViewModel;
                        tmpDialog.ShowDialog();
                    }
                }
            }
            catch (Exception Ex)
            {
                
                //throw;
            }
        }

        void Settings_NewEditDataSourceViewModel_ViewModel_DeleteClick(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }  

        void Settings_NewEditDataSourceViewModel_ViewModel_ApplyClick(object sender, EventArgs e)
        {
            try
            {
                //fire event to apply data
                if (ValidateInputs() == true) if (DataChanged != null) DataChanged(this, EventArgs.Empty);
                _DataChanged = false;
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Error on applying Data!", Ex, true, EMessageTypes.Error);
            }
        }

        void Settings_NewEditDataSourceViewModel_ViewModel_OkClick(object sender, EventArgs e)
        {
            try
            {
                Settings_NewEditDataSource parentDlg = null;
                if ((Settings_NewEditDataSource)((CDelegatingCommand)Cmd_OkClick.Command).CommandParameter != null)
                {
                    parentDlg = (Settings_NewEditDataSource)((CDelegatingCommand)Cmd_OkClick.Command).CommandParameter;

                    if (parentDlg != null)
                    {
                        if (ValidateInputs() == true)
                        {
                            parentDlg.DialogResult = true;
                            parentDlg.Close();
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage(LanguageStrings.GetText("Error on applying Data!"), Ex, EMessageTypes.Error);
            } 
        }

        void Settings_NewEditDataSourceViewModel_ViewModel_CloseClick(object sender, EventArgs e)
        {
            try
            {
                Settings_NewEditDataSource parentDlg = null;
                if ((Settings_NewEditDataSource)((CDelegatingCommand)Cmd_CloseClick.Command).CommandParameter != null)
                {
                    parentDlg = (Settings_NewEditDataSource)((CDelegatingCommand)Cmd_CloseClick.Command).CommandParameter;

                    if (_DataChanged == true)
                    {
                        if (System.Windows.MessageBox.Show(QuestionMessages.QUESTION_CLOSE_DIALOG__LOSE_CHANGES, "Question",
                            System.Windows.MessageBoxButton.YesNo) == System.Windows.MessageBoxResult.No)
                            return;
                    }

                    if (parentDlg != null)
                        parentDlg.Close();

                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage(LanguageStrings.GetText("Can't Close Dialog!"), Ex, EMessageTypes.Error);
            } 
        }

        void Cmd_Exec_EditOption()
        {
            try
            {
                if (SelectedAssembly.Length > 0 && SelectedClass.Length > 0 && SelectedOption != null)
                {
                    System.Windows.Controls.ListView tmpListView = null;
                    if ((System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_EditOptionClick.Command).CommandParameter != null)
                    {
                        tmpListView = (System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_EditOptionClick.Command).CommandParameter;
                        OptionInfo tmpOptionInfo = _AssemblyMetaData[SelectedAssembly].ElementOptions.GetClassOptionInfo(SelectedClass, SelectedOption.Key);
                        //if (tmpOptionInfo != null)
                        {
                            Settings_NewEditDataSourceExtensionViewModel tmpViewModel = new Settings_NewEditDataSourceExtensionViewModel(SelectedOption, tmpOptionInfo);
                            Settings_EditDataSourceExtension tmpDialog = new Settings_EditDataSourceExtension();
                            tmpDialog.DataContext = tmpViewModel;
                            if (tmpDialog.ShowDialog() == true)
                            {
                                SelectedOption = ((Settings_NewEditDataSourceExtensionViewModel)tmpDialog.DataContext).Data;
                                if (tmpListView != null) tmpListView.Items.Refresh();
                            }
                        }
                        //else
                        //    System.Windows.MessageBox.Show("Unable to visible selected Option!", "Hint", System.Windows.MessageBoxButton.OK);
                    }
                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage(LanguageStrings.GetText("Error on Changing Option-Value!"), Ex, EMessageTypes.Error);
            }
        }

        void Cmd_Exec_AddAssignment()
        {
            try 
            {
                 System.Windows.Controls.ListView tmpListView = null;
                 if ((System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_AddAssignmentClick.Command).CommandParameter != null)
                 {
                     tmpListView = (System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_AddAssignmentClick.Command).CommandParameter;
                     Settings_NewEditDataSourceExtensionViewModel tmpViewModel = new Settings_NewEditDataSourceExtensionViewModel(null);
                     Settings_EditDataSourceExtension oEditExtension = new Settings_EditDataSourceExtension();
                     oEditExtension.DataContext = tmpViewModel;
                     if (oEditExtension.ShowDialog() == true)
                     {

                         if (CanAddAssignment(((Settings_NewEditDataSourceExtensionViewModel)oEditExtension.DataContext).Data) == true)
                         {
                             CustomAssignments.Add(((Settings_NewEditDataSourceExtensionViewModel)oEditExtension.DataContext).Data);

                             tmpListView.Items.Refresh();
                         }
                         else
                         {
                             System.Windows.MessageBox.Show("Key - Value - Assignment already exists and can't add!", "Info", System.Windows.MessageBoxButton.OK);
                         }
                     }
                 }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage(ErrorMessages.USERSETTINGS__ERROR_UNABLE_TO_ADD_FILE_EXTENSION, Ex, true, EMessageTypes.Error);
            }
        }

        void Cmd_Exec_EditAssignment()
        {
            try
            {
                if (SelectedAssignment != null)
                {
                    System.Windows.Controls.ListView tmpListView = null;
                    if ((System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_EditAssignmentClick.Command).CommandParameter != null)
                    {
                        tmpListView = (System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_EditAssignmentClick.Command).CommandParameter;
                        Settings_NewEditDataSourceExtensionViewModel tmpViewModel = new Settings_NewEditDataSourceExtensionViewModel(SelectedAssignment, null);
                        Settings_EditDataSourceExtension oEditExtension = new Settings_EditDataSourceExtension();
                        oEditExtension.DataContext = tmpViewModel;
                        if (oEditExtension.ShowDialog() == true)
                        {
                            SelectedAssignment = ((Settings_NewEditDataSourceExtensionViewModel)oEditExtension.DataContext).Data;
                            if (tmpListView != null) tmpListView.Items.Refresh();
                        }
                    }
                }

            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage(ErrorMessages.USERSETTINGS__ERROR_UNABLE_TO_EDIT_FILE_EXTENSION, Ex, true, EMessageTypes.Error);
            }
        }

        void Cmd_Exec_DeleteAssignment()
        {
            try
            {
                if (SelectedAssignment != null)
                {
                    System.Windows.Controls.ListView tmpListView = null;
                    if ((System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_EditAssignmentClick.Command).CommandParameter != null)
                    {
                        tmpListView = (System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_EditAssignmentClick.Command).CommandParameter;

                        string sMessage = LanguageStrings.GetText(QuestionMessages.QUESTION_NEW_EDIT_ASSIGNMENT__DELETE_ITEM, WMSSoft.Lib.Enums.EStringTypes.stQuestion);
                        sMessage = sMessage.Replace("###NAME###", SelectedAssignment.Key);
                        if (System.Windows.MessageBox.Show(sMessage, LanguageStrings.GetText("Question"), 
                                                           System.Windows.MessageBoxButton.YesNo) == System.Windows.MessageBoxResult.Yes)
                        {
                            CustomAssignments.Remove(SelectedAssignment);                            

                            if (tmpListView != null) tmpListView.Items.Refresh();
                        }
                    }
                }

            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage(ErrorMessages.USERSETTINGS__ERROR_UNABLE_TO_DELETE_FILE_EXTENSION, Ex, true, EMessageTypes.Error);
            }
        }

        #endregion

        #region Helper Functions

        void LoadOptions()
        {
            try
            {
                if (_DataSourceOptions == null) _DataSourceOptions = new List<KeyValueAssignment>();
                _DataSourceOptions.Clear();

                Dictionary<string, KeyValueAssignment>.KeyCollection tmpKeys = _Data.OptionsDict.Keys;
                foreach (string Key in tmpKeys)
                {
                    _DataSourceOptions.Add(_Data.OptionsDict[Key]);
                }
            }
            catch (Exception Ex)
            {
                throw new Exception("Could not load DataSourceOptions!", Ex);
            }
        }

        void Settings_NewEditDataSourceViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            try
            {
                if (e.PropertyName == "SelectedAssembly")
                {
                    //TODO: read classes in selected assembly
                    AssemblyClasses = LoadDataSourceClases(SelectedAssembly);
                }
                else if (e.PropertyName == "SelectedClass")
                {
                    if (SelectedAssembly.Length > 0 && SelectedClass.Length > 0)
                    {
                        List<string> tmpClassOptionList = _AssemblyMetaData[SelectedAssembly].ElementOptions.GetClassOptions(SelectedClass);
                        string OptionValue = "";
                        if (tmpClassOptionList != null)
                        {

                            DataSourceOptions.Clear();
                            List<KeyValueAssignment> tmpClassOption = new List<KeyValueAssignment>();
                            foreach (string Item in tmpClassOptionList)
                            {
                                OptionValue = _AssemblyMetaData[SelectedAssembly].ElementOptions.GetClassOptionInfo(SelectedClass, Item).Default;
                                tmpClassOption.Add(new KeyValueAssignment(Item, OptionValue));
                            }
                            DataSourceOptions = null;
                            DataSourceOptions = tmpClassOption;                             
                        }
                    }
                }
                else if (e.PropertyName == "SelectedOption")
                {
                    if (_SelectedOption != null)
                        OptionSelected = true;
                    else
                        OptionSelected = false;
                }
                else if (e.PropertyName == "SelectedCustomItem")
                {
                    if (_SelectedCustomItem != null)
                        CustomItemSelected = true;
                    else
                        CustomItemSelected = false;
                }
                else if (e.PropertyName == "SelectedAssignment")
                {
                    if (SelectedAssignment != null)
                        CustomAssignmentSelected = true;
                    else
                        CustomAssignmentSelected = false;
                }
            }
            catch (Exception Ex)
            {
                
            }
        }

        List<KeyValueAssignment> LoadInstallableDataSources()
        {
            try
            {
                _AssemblyMetaData = null;
                List<KeyValueAssignment> tmpAssembies = new List<KeyValueAssignment>();

                //Add Default selection
                tmpAssembies.Add(new KeyValueAssignment("", LanguageStrings.GetText("Please select a Assembly...")));

                _AssemblyMetaData = CMetaDataReader.ReadAssemblies(cUserSettingsReader.DataSourcesPath);

                if (_AssemblyMetaData != null)
                {
                    Dictionary<string, CMeta>.KeyCollection oTmpInstallableDataSourceKeys = _AssemblyMetaData.Keys;
                    foreach (string KeyItem in oTmpInstallableDataSourceKeys)
                    {
                        tmpAssembies.Add(new KeyValueAssignment(KeyItem, KeyItem));
                    }
                }

                return tmpAssembies;
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage(ErrorMessages.USERSETTINGS__ERROR_EDIT_DATASOURCE_LOAD_INSTALLABLE_DATASOURCES_FAILED, Ex, true, EMessageTypes.Error);
                return null;
            }
        }

        void LoadCustomItems()
        {
            try
            {
                if (CustomItems == null) CustomItems = new List<cStartmenuItem>();
                CustomItems.Clear();

                for (int iItemCounter = 0; iItemCounter < _Data.Items.Count; iItemCounter++)
                {
                    CustomItems.Add((cStartmenuItem)_Data.Items[iItemCounter]);
                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Unable to load Custom DataSource-Items!", Ex, true, EMessageTypes.Error);
            }
        }

        void LoadAssignments()
        {
            try
            {

                if (CustomAssignments == null) CustomAssignments = new List<KeyValueAssignment>();

                Dictionary<string, KeyValueAssignment>.KeyCollection oExtLinkKeys = Data.ExtensionsDict.Keys;
                foreach (string sKeyItem in oExtLinkKeys)
                {                    
                    CustomAssignments.Add(Data.ExtensionsDict[sKeyItem]);
                }
            }
            catch (Exception Ex)
            {

            }
        }

        List<KeyValueAssignment> LoadDataSourceClases(string Assembly)
        {
            try
            {
                List<KeyValueAssignment> tmpClassesList = new List<KeyValueAssignment>();
                //_AssemblyMetaData

                tmpClassesList.Add(new KeyValueAssignment("", LanguageStrings.GetText("Please select a Class...")));

                if (Assembly != "")
                {


                    if (_AssemblyMetaData != null)
                    {
                        if (_AssemblyMetaData.ContainsKey(Assembly) == true)
                        {
                            foreach (CMetaClass ClassItem in _AssemblyMetaData[Assembly].Classes)
                            {                                
                                tmpClassesList.Add(new KeyValueAssignment(ClassItem.Name, ClassItem.Name));
                            }
                        }
                    }
                }
                return tmpClassesList;
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage(ErrorMessages.USERSETTINGS__ERROR_EDIT_DATASOURCE_LOAD_DATASOURCE_CLASSES_FAILED, Ex, true, EMessageTypes.Error);
                return null;
            }
        }

        bool CanAddAssignment(KeyValueAssignment Value)
        {
            try
            {
                var ListResult = from Item in CustomAssignments where Item.Key.ToUpper() == Value.Key.ToUpper() && 
                                                                      Item.Value.ToUpper() == Value.Value.ToUpper() select Item;
                if (ListResult != null && ListResult.Count() > 0)
                    return false;
                else
                    return true;
            }
            catch (Exception Ex)
            {
                throw new Exception("Unable to verify if you can add new Assignment!", Ex);
            }
        }

        bool ValidateInputs()
        {
            if (DataSourceName.Length > 0 && DataSourceShortName.Length > 0 &&
                SelectedAssembly.Length > 0 && SelectedClass.Length > 0) 
            {
                //TODO: apply values to DataSource element

                Data.Name = DataSourceName;
                Data.ShortName = DataSourceShortName;
                Data.Assembly = SelectedAssembly;
                Data.ClassName = SelectedClass;

                Data.OptionsDict.Clear();
                foreach (KeyValueAssignment Option in DataSourceOptions)
                {
                    //if (Data.OptionsDict.ContainsKey(Option.Key) == true)
                    {
                        Data.OptionsDict.Add(Option.Key, Option);
                        //Data.OptionsDict[Option.Key] = Option;
                    }
                }

                Data.ExtensionsDict.Clear();
                foreach (KeyValueAssignment Assignment in CustomAssignments)
                {
                    Data.ExtensionsDict.Add(Assignment.Key, Assignment);
                }

                Data.Items.Clear();
                foreach (cStartmenuItem CustomItem in CustomItems) 
                {
                    Data.Items.Add(CustomItem);
                }

                return true;
            }                
            else
                return false;
        }
        #endregion
    }
}
